<?
// class comment review
class comment_review{
	var $table = "comment_review";
	var $like = 0;
	var $arr_cor_review = array();
	var $page = 10;
	//hiển thị bình luận của bài review;
	function show_comment($review_id,$start = 0){
		if(isset($review_id) && $review_id > 0){
			$db_show_comment = new db_query("SELECT cor_id,cor_review_id,cor_content,cor_parent,cor_user_id,cor_like,cor_date
														FROM ".$this->table."
														WHERE cor_review_id = " . $review_id."
														ORDER BY cor_date DESC
														LIMIT ".$start.",".$this->page);
			if(mysql_num_rows($db_show_comment->result) > 0){
				
				while($review_comment = mysql_fetch_assoc($db_show_comment->result)){
					$this->arr_cor_review[] = $review_comment;
				}
			}
			unset($db_show_comment);
		}
		return $this->arr_cor_review;
	}
	//show phan hoi cua comment
	//$parent_id la id cua comment co phan hoi
	function show_reply_comment($parent_id){
		if(isset($parent_id) && $parent_id > 0){
			$db_show_reply = new db_query("SELECT cor_id,cor_review_id,cor_content,cor_parent,cor_user_id,cor_like,cor_date
														FROM ".$this->table."
														WHERE cor_parent = ".$parent_id."
														ORDER BY cor_date DESC");
			if(mysql_num_rows($db_show_reply->result) > 0){
				while($row_reply = mysql_fetch_assoc($db_show_reply->result)){
					$this->arr_con_news[] = $row_reply;
				}
			}
			unset($db_show_reply);
		}
		return $this->arr_con_news;
	}
	//function like bình luận
	//$review_comment_id LÀ ID CỦA BÌNH LUẬN ĐC LIKE
	function like_comment($review_comment_id){
		if(isset($review_comment_id) && $review_comment_id >0){
			$db_select_cr = new db_query("SELECT cor_id,cor_user_id,cor_like 
														FROM ".$this->table." 
														WHERE cor_id =".$review_comment_id);
			if(mysql_num_rows($db_select_cr->result) > 0){
				if($cm_like = mysql_fetch_assoc($db_select_cr->result)){
					$like = $cm_like['cor_like'] + 1;
					$db_insert = new db_execute("UPDATE ".$this->table." SET cor_like = " . $like . " WHERE cor_id = ".$review_comment_id);
					unset($db_insert);
				}
			}
			unset($db_select_cr);
		}
		return $like;
	}
	//function thêm mới bình luận
	function add_comment($cor_review_id,$cor_content,$cor_user_id){
		if($cor_review_id > 0 && $cor_content != "" && $cor_user_id > 0){
			$cor_date = time();
			$cor_content = htmlspecialchars($cor_content);
			$db_insert = new db_execute("INSERT INTO ".$this->table."(cor_review_id,cor_content,cor_user_id,cor_date) 
												VALUES(".$cor_review_id.",'".$cor_content."',".$cor_user_id.",".$cor_date.")");
			unset($db_insert);
		}
	}
	//function trả lời bình luận
	function reply_comment($cor_id,$cor_review_id,$cor_content,$cor_user_id){
		if($cor_review_id > 0 && $cor_content != "" && $cor_user_id > 0 && $cor_id > 0){
			$cor_date = time();
			$cor_content = htmlspecialchars($cor_content);
			$db_insert = new db_execute("INSERT INTO ".$this->table."(cor_review_id,cor_content,cor_user_id,cor_parent,cor_date) 
												VALUES(".$cor_review_id.",'".$cor_content."',".$cor_user_id.",".$cor_id.",".$cor_date.")");
			unset($db_insert);
		}
	}
}
//class comment news
class comment_news{
	var $table = "comment_news";
	var $like = 0;
	var $arr_con_news = array();
	var $page = 10;
	//hiển thị tat ca bình luận của bài tin tuc;
	function show_comment($news_id,$start = 0){
		if(isset($news_id) && $news_id > 0){
			$db_show_comment = new db_query("SELECT con_id,con_news_id,con_content,con_user_id,con_parent,con_like,con_date
														FROM ".$this->table."
														WHERE con_news_id = " . $news_id."
														ORDER BY con_date DESC
														LIMIT ".$start.",".$this->page);
			if(mysql_num_rows($db_show_comment->result) > 0){
				
				while($new_comment = mysql_fetch_assoc($db_show_comment->result)){
					$this->arr_con_news[] = $new_comment;
				}
			}
			unset($db_show_comment);
		}
		return $this->arr_con_news;
	}
	//show phan hoi cua comment
	//$parent_id la id cua comment co phan hoi
	function show_reply_comment($parent_id){
		if(isset($parent_id) && $parent_id > 0){
			$db_show_reply = new db_query("SELECT con_id,con_content,con_user_id,con_parent,con_like,con_date
														FROM ".$this->table."
														WHERE con_parent = ".$parent_id."
														ORDER BY con_date DESC");
			if(mysql_num_rows($db_show_reply->result) > 0){
				while($row_reply = mysql_fetch_assoc($db_show_reply->result)){
					$this->arr_con_news[] = $row_reply;
				}
			}
			unset($db_show_reply);
		}
		return $this->arr_con_news;
	}
	//function like bình luận
	//$news_comment LÀ ID CỦA BÌNH LUẬN ĐC LIKE
	function like_comment($news_comment_id){
		if(isset($news_comment_id) && $news_comment_id > 0){
			$db_select_cr = new db_query("SELECT con_id,con_user_id,con_like 
														FROM ".$this->table." 
														WHERE con_id =".$news_comment_id);
			if(mysql_num_rows($db_select_cr->result) > 0){
				if($cm_like = mysql_fetch_assoc($db_select_cr->result)){
					$like = $cm_like['con_like'] + 1;
					$db_update = new db_execute("UPDATE ".$this->table." SET con_like = " . $like . " WHERE con_id = ".$news_comment_id);
					unset($db_update);
				}
			}
			unset($db_select_cr);
		}
		return $like;
	}
	//function thêm mới bình luận
	function add_comment($con_news_id,$con_content,$con_user_id){
		if($con_news_id > 0 && $con_content != "" && $con_user_id > 0){
			$con_date = time();
			$con_content = htmlspecialchars($con_content);
			$db_insert = new db_execute("INSERT INTO ".$this->table."(con_news_id,con_content,con_user_id,con_date) 
												VALUES(".$con_news_id.",'".$con_content."',".$con_user_id.",".$con_date.")");
			unset($db_insert);
		}
	}
	//function trả lời bình luận
	function reply_comment($con_id,$con_news_id,$con_content,$con_user_id){
		if($con_news_id > 0 && $con_content != "" && $con_user_id > 0 && $con_id > 0){
			$con_date = time();
			$con_content = htmlspecialchars($con_content);
			$db_insert = new db_execute("INSERT INTO ".$this->table."(con_news_id,con_content,con_user_id,con_parent,con_date) 
												VALUES(".$con_news_id.",'".$con_content."',".$con_user_id.",".$con_id.",".$con_date.")");
			unset($db_insert);
		}
	}
}


//class comment user_review
class comment_user_review{
	var $table = "comment_user_review";
	var $like = 0;
	var $arr_con_news = array();
	//hiển thị tat ca bình luận của bài tin tuc;
	function show_comment($user_review_id,$start = 0){
		if(isset($user_review_id) && $user_review_id > 0){
			$db_show_comment = new db_query("SELECT cou_id,cou_content,cou_user_review_id,cou_user_id,cou_like,cou_date
														FROM ".$this->table."
														WHERE cou_user_review_id = " . $user_review_id."
														ORDER BY cou_date DESC");
			if(mysql_num_rows($db_show_comment->result) > 0){
				
				while($new_comment = mysql_fetch_assoc($db_show_comment->result)){
					$this->arr_con_news[] = $new_comment;
				}
			}
			unset($db_show_comment);
		}
		return $this->arr_con_news;
	}
	//function like bình luận
	//$user_review_id LÀ ID CỦA BÌNH LUẬN ĐC LIKE
	function like_comment($user_review_id){
		if(isset($user_review_id) && $user_review_id >0){
			$db_select_cr = new db_query("SELECT cou_id,cou_user_id,cou_like 
														FROM ".$this->table." 
														WHERE cou_id =".$user_review_id);
			if(mysql_num_rows($db_select_cr->result) > 0){
				if($cm_like = mysql_fetch_assoc($db_select_cr->result)){
					$like = $cm_like['cou_like'] + 1;
					$db_update = new db_execute("UPDATE ".$this->table." SET cou_like = " . $like . " WHERE cou_id = ".$user_review_id);
					unset($db_update);
				}
			}
			unset($db_select_cr);
		}
		return $like;
	}
	//function thêm mới bình luận
	function add_comment($cou_user_review_id,$cou_content,$cou_user_id){
		if($cou_user_review_id > 0 && $cou_content != "" && $cou_user_id > 0){
			$cou_date = time();
			$cou_content = htmlspecialchars($cou_content);
			$db_insert = new db_execute("INSERT INTO ".$this->table."(cou_user_review_id,cou_content,cou_user_id,cou_date) 
												VALUES(".$cou_user_review_id.",'".$cou_content."',".$cou_user_id.",".$cou_date.")");
			unset($db_insert);
		}
	}
}
?>
